﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCCN.DAL;
using SCCN.BLL;
using SCCN.Utility;

public partial class Admin_AdSpaceList : System.Web.UI.Page
{
    AdSpaceBLL bllAdSpace = AdSpaceBLL.getInstance();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDrp();
            BindRpt();
        }
    }

    void BindDrp()
    {
        this.drpParentAd.DataSource = bllAdSpace.GetAdSpaceByParentId(0);
        this.drpParentAd.DataTextField = "AdSpaceName";
        this.drpParentAd.DataValueField = "Id";
        this.drpParentAd.DataBind();
        this.drpParentAd.Items.Insert(0, new ListItem("站点", "0"));
        this.drpParentId.DataSource = bllAdSpace.GetAdSpaceByParentId(0);
        this.drpParentId.DataTextField = "AdSpaceName";
        this.drpParentId.DataValueField = "Id";
        this.drpParentId.DataBind();
        this.drpParentId.Items.Insert(0, new ListItem("站点", "0"));
    }

    void BindRpt()
    {
        SC_AdSpace obj = new SC_AdSpace();
        if (this.txtAdSpaceName.Text.Length > 0)
        {
            obj.AdSpaceName = this.txtAdSpaceName.Text;
        }
        else
        {
            obj.AdSpaceName = string.Empty;
        }
        if (this.drpParentId.SelectedIndex >= 0)
        {
            obj.ParentId = Convert.ToInt32(this.drpParentId.SelectedValue);
        }
        else
        {
            obj.ParentId = 0;
        }
        obj.IsDelete = false;
        int rowCount = 0;
        this.rptList.DataSource = bllAdSpace.GetAdSpaceByCondition(obj, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out rowCount);
        this.AspNetPager1.RecordCount = rowCount;
        this.rptList.DataBind();
    }


    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindRpt();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        this.drpParentAd.SelectedIndex = -1;
        this.txtAdName.Text = string.Empty;
        this.txtAdSpaceDesc.Text = string.Empty;
        this.Hid.Value = string.Empty;
    }


    protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
    { 
        int id = 0;
        id = Convert.ToInt32(e.CommandArgument);
        if (e.CommandName == "Del")
        {
            if (bllAdSpace.Delete(id))
            {
                HttpHelper.Alert("删除成功！", Page);
                BindRpt();
            }
        }
        if (e.CommandName == "Update")
        {
            SC_AdSpace obj = bllAdSpace.GetAdSpaceById(id);
            this.txtAdName.Text = obj.AdSpaceName;
            this.drpParentAd.SelectedValue = obj.ParentId.ToString();
            this.Hid.Value = obj.Id.ToString();
            this.txtAdSpaceDesc.Text = obj.AdSpaceDesc;
        }
        if (e.CommandName == "Edit")
        {
            Response.Redirect("AdvertisementList.aspx?adspaceid=" + id);
        }


    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (this.Hid.Value.Length == 0)
        {
            SC_AdSpace obj = new SC_AdSpace();
            obj.AdSpaceName = this.txtAdName.Text;
            obj.ParentId = Convert.ToInt32(this.drpParentAd.SelectedValue);
            obj.IsDelete = false;
            obj.AdSpaceDesc = this.txtAdSpaceDesc.Text;
            if (bllAdSpace.Add(obj))
            {
                HttpHelper.Alert("保存成功！", Page);
                BindRpt();
                BindDrp();
                this.txtAdName.Text = string.Empty;
                this.txtAdSpaceName.Text = string.Empty;
                this.drpParentAd.SelectedIndex = -1;
                this.drpParentId.SelectedIndex = -1;
                this.txtAdSpaceDesc.Text = string.Empty;
            }
            else
            {
                HttpHelper.Alert("保存失败！", Page); 
            }
        }
        else
        {
            SC_AdSpace obj = bllAdSpace.GetAdSpaceById(Convert.ToInt32(this.Hid.Value));
            obj.AdSpaceName = this.txtAdName.Text;
            obj.ParentId = Convert.ToInt32(this.drpParentAd.SelectedValue);
            obj.IsDelete = false;
            obj.AdSpaceDesc = this.txtAdSpaceDesc.Text;
            if (bllAdSpace.Update(obj))
            {
                HttpHelper.Alert("保存成功！", Page);
                BindRpt();
                BindDrp();
                this.txtAdName.Text = string.Empty;
                this.txtAdSpaceName.Text = string.Empty;
                this.drpParentAd.SelectedIndex = -1;
                this.drpParentId.SelectedIndex = -1;
                this.txtAdSpaceDesc.Text = string.Empty;
            }
            else
            {
                HttpHelper.Alert("保存失败！", Page);
            }
        }
    }
}